package com.eralink.unionuhfreader.ui;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.alibaba.fastjson.JSON;
import com.eralink.unionuhfreader.R;
import com.eralink.unionuhfreader.adapter.ProductOrderAdapter;
import com.eralink.unionuhfreader.adapter.RecycleViewDivider;
import com.eralink.unionuhfreader.base.BaseActivity;
import com.eralink.unionuhfreader.bean.ProductOrder;
import com.eralink.unionuhfreader.databinding.ActivityOrderBinding;
import com.eralink.unionuhfreader.http.Api;
import com.eralink.unionuhfreader.http.JsonCallBack;
import com.eralink.unionuhfreader.http.PageResult;
import com.eralink.unionuhfreader.http.Result;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BasePopupView;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.List;

public class OrderActivity extends BaseActivity {
    private static  final String TAG=OrderActivity.class.getSimpleName();
    ActivityOrderBinding bd;
    int pageNo=1;
    int pagesize=50;
    private ProductOrderAdapter orderAdapter;
    private LinearLayoutManager linearLayoutManager;
    private List<ProductOrder> productOrders=new ArrayList<>();
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        bd= DataBindingUtil.setContentView(this, R.layout.activity_order);
        orderAdapter=new ProductOrderAdapter(this,productOrders);
        linearLayoutManager=new LinearLayoutManager(this);
        bd.recyclerView.setLayoutManager(linearLayoutManager);
        bd.recyclerView.addItemDecoration(new RecycleViewDivider(this, LinearLayoutManager.HORIZONTAL, R.drawable.item_divider));
        bd.recyclerView.setAdapter(orderAdapter);

        bd.swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
             pagesize=1;
             productOrders.clear();
             orderAdapter.notifyDataSetChanged();
             getorder();
            }
        });
        bd.recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                if (layoutManager != null && layoutManager.findLastCompletelyVisibleItemPosition() == layoutManager.getItemCount() - 1) {
                    // 到达列表底部，触发加载更多操作
                    //loadMoreData(); // 假设这是一个异步方法，用于获取更多数据并更新UI
                    pageNo++;
                    getorder();
                }
            }
        });
        getorder();
    }

    private void getorder(){
        BasePopupView popupView = new XPopup.Builder(this)
                .asLoading("正在加载数据....")
                .show();
        Api.getorders(pageNo,pagesize,new JsonCallBack<Result<PageResult<ProductOrder>>>(){
            @Override
            public void onFinish() {
                super.onFinish();
                bd.swipeRefreshLayout.setRefreshing(false);
                if (popupView.isShow()){
                    popupView.dismiss();
                }
            }

            @Override
            public void onSuccess(Response<Result<PageResult<ProductOrder>>> response) {
                Result<PageResult<ProductOrder>> result=response.body();
                Log.d(TAG, JSON.toJSONString(result));
                if (result.isSucess()){
                    if (result.getData().getList()!=null&&result.getData().getList().size()>0){
                        productOrders.addAll(result.getData().getList());
                        orderAdapter.notifyDataSetChanged();
                    }
                }
            }
        });
    }
}
